﻿using System.Collections.Generic;
using System.Linq;
using Missbot.Common;
using Missbot.Service;
using Dolany.UtilityTool;

namespace Missbot.AIModule.Gaming.Lottery
{
    /// <summary>
    /// 开箱子服务
    /// </summary>
    public class LotterySvc : ISingleton, IDataMgr
    {
        /// <summary>
        /// 奖项列表
        /// </summary>
        private List<LotteryBonus> Models;

        /// <summary>
        /// 开箱子费用
        /// </summary>
        public const int LotteryFee = 100;

        public LotteryBonus this[string Name] => Models.FirstOrDefault(p => p.Name == Name);

        /// <summary>
        /// 获取一个随机奖项
        /// </summary>
        /// <returns></returns>
        public LotteryBonus RandLottery()
        {
            return Models.ToDictionary(p => p, p => p.Rate).RandRated();
        }

        public void RefreshData()
        {
            Models = LotteryBonusDA.GetAll();
        }
    }
    
    /// <summary>
    /// 开箱子奖项
    /// </summary>
    public class LotteryBonus : DbBaseEntity
    {
        /// <summary>
        /// 奖项名称
        /// </summary>
        public string Name { get; set; }
        
        /// <summary>
        /// 奖励数额
        /// </summary>
        public int Bonus { get; set; }
        
        /// <summary>
        /// 稀有率
        /// </summary>
        public int Rate { get; set; }
        
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }
    }

    public static class LotteryBonusDA
    {
        /// <summary>
        /// 获取所有奖项列表
        /// </summary>
        /// <returns></returns>
        public static List<LotteryBonus> GetAll()
        {
            return MongoService<LotteryBonus>.Get();
        }
        
        /// <summary>
        /// 打印奖励信息
        /// </summary>
        /// <returns></returns>
        public static string Print(this LotteryBonus entity)
        {
            return $"【{entity.Name}】\n    {entity.Description}\n你获得了 {entity.Bonus.CurencyFormat()}";
        }
    }
}